package com.lc.railway.ui.activity;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.railway.R;
import com.lc.railway.app.AppActivity;
import com.lc.railway.http.request.ApiDutyIndexApi;
import com.lc.railway.http.response.DutyBean;
import com.lc.railway.manager.InputTextManager;
import com.lc.railway.ui.dialog.FieldsDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DutyIndexActivity extends AppActivity {
    @BindView(R.id.title_bar_top)
    TitleBar titleBarTop;
    @BindView(R.id.tv_year)
    TextView tvYear;
    @BindView(R.id.tv_month)
    TextView tvMonth;
    @BindView(R.id.tv_day)
    TextView tvDay;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private List<String> years = new ArrayList<>();
    private List<String> months = new ArrayList<>();
    private List<String> days = new ArrayList<>();
    private String nian;
    private String yue;
    private String ri;
    private List<DutyBean.DataBeanX.DataBean> datas=new ArrayList<>();
    private BaseQuickAdapter<DutyBean.DataBeanX.DataBean, BaseViewHolder> adapter;
    private int mPage=1;
    private int total;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_dutyindex;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        titleBarTop.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {
                FieldsDialog dialog=new FieldsDialog(getContext(),DutyIndexActivity.this,"值班列表");
                dialog.show();
            }
        });
        nian = Calendar.getInstance().get(Calendar.YEAR)+"";
        yue = (Calendar.getInstance().get(Calendar.MONTH))+1+"";
        ri = Calendar.getInstance().get(Calendar.DAY_OF_MONTH)+"";
             tvYear.setText(nian);
             tvMonth.setText(yue);
             tvDay.setText(ri);
        api_duty_index(mPage);
        adapter=new BaseQuickAdapter<DutyBean.DataBeanX.DataBean, BaseViewHolder>(R.layout.item_duty,datas) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, DutyBean.DataBeanX.DataBean dataBean) {
                holder.setText(R.id.tv_date,dataBean.getDate());
                holder.setText(R.id.tv_leader,dataBean.getLead());
                holder.setText(R.id.tv_cadres,dataBean.getCadre());
                holder.setText(R.id.tv_attendant,dataBean.getPeople());
                holder.setText(R.id.tv_police,dataBean.getDate());
                holder.setText(R.id.tv_phone, Html.fromHtml(dataBean.getMobile()));
                holder.setText(R.id.tv_driver,dataBean.getDriver());


            }
        };
        View emptyview = View.inflate(this, R.layout.empty_emptylayout, null);
        adapter.setEmptyView(emptyview);
        recyclerView.setAdapter(adapter);
        InputTextManager.with(this)
                .addView(tvYear)
                .addView(tvMonth)
                .addView(tvDay)
                .setMain(btnLogin)
                .build();
        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(final @NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(() -> {
                    mPage++;
                    if (mPage <total) {
                        api_duty_index_more(mPage);

                        refreshLayout.finishLoadMore();
                    } else {
                        toast("数据全部加载完毕");
                        refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                    }
                }, 1000);
            }

            @Override
            public void onRefresh(final @NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(() -> {
                    mPage = 1;
                    api_duty_index(mPage);
                    refreshLayout.finishRefresh();
                }, 1000);

            }
        });
    }

    private void api_duty_index_more(int mPage) {
        EasyHttp.post(this)
                .api(new ApiDutyIndexApi().setPage(mPage).setYear(nian).setMoon(yue).setDay(ri))
                .request(new HttpCallback<DutyBean>(this) {
                    @Override
                    public void onSucceed(DutyBean data) {
//
                        if (data.getError_code() == 200) {
                            datas.addAll(data.getData().getData());
                            total=data.getData().getTotal();
                            adapter.setNewData(datas);
                            adapter.notifyDataSetChanged();

                        }
                    }
                });
    }


    private void api_duty_index(int page) {
        EasyHttp.post(this)
                .api(new ApiDutyIndexApi().setPage(page).setYear(nian).setMoon(yue).setDay(ri))
                .request(new HttpCallback<DutyBean>(this) {
                    @Override
                    public void onSucceed(DutyBean data) {
//
                        if (data.getError_code() == 200) {
                            datas.clear();
                            datas.addAll(data.getData().getData());
                            total=data.getData().getTotal();
                            adapter.setNewData(datas);
                            adapter.notifyDataSetChanged();

                        }
                    }
                });

    }

    @OnClick({R.id.ll_year, R.id.ll_month, R.id.ll_day, R.id.btn_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_year:
                years.clear();
                Calendar cal = Calendar.getInstance();

                int year = cal.get(Calendar.YEAR);

                for (int i = 1; i < year - 1999; i++) {
                    years.add((2000 + i) + "");
                }
                OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        nian = years.get(options1);
                        tvYear.setText(nian);

                    }
                }).build();

//            pvOptions.setTitleText("求职状态");
                Collections.reverse(years);
                pvOptions.setPicker(years);
                pvOptions.show();
                break;
            case R.id.ll_month:
                for (int i = 1; i < 13; i++) {
                    months.add(i + "");
                }
                OptionsPickerView pvOptions1 = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        yue = months.get(options1);
                        tvMonth.setText(yue);
                    }
                }).build();

//            pvOptions.setTitleText("求职状态");
                pvOptions1.setPicker(months);
                pvOptions1.show();
                break;
            case R.id.ll_day:
                for (int i = 1; i < 32; i++) {
                    days.add(i + "");
                }
                OptionsPickerView pvOptions2 = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        ri = days.get(options1);
                        tvDay.setText(ri);

                    }
                }).build();

//            pvOptions.setTitleText("求职状态");
                pvOptions2.setPicker(days);
                pvOptions2.show();
                break;
            case R.id.btn_login:
                api_duty_index(mPage);
                break;
        }
    }
}
